﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Functions2</title>
</head>
<body>
    <ul>
        Functions2
        <li>
            The throw statement interrupts execution of the function. It should be given an exception object containing
            <ul>
                <li>name property that identifies the type of the exception</li>
                <li>a descriptive message property</li>
            </ul>
        </li>
    </ul>
    <script type="text/javascript">
        var initialization = function () {
            add = function (a, b) {
                if (typeof a !== 'number' || typeof b !== 'number') {
                    throw {
                        name: 'TypeError',
                        message: 'add needs numbers'
                    }
                }
                return a + b;
            }

            var augmentingTypes = function () {
                Function.prototype.method = function (name, func) {
                    this.prototype[name] = func;
                    return this;
                };
                String.method('trim', function () {
                    return this.replace(/^\s+|\s$/g, ''); //empieza en espacio+ o termina en espacio+
                });
            }();
        }();

        var functions = {
            tryIt: function () {
                try {
                    add("seven");
                    return null;
                } catch (e) {
                    return 'Example message is:' + e.name + ': ' + e.message;
                }
            }(),
            trimAText: ' asdjkj '.trim(),
            scope: function () {
                var a = 3, b = 5;

                var bar = function () {
                    var b = 7, c = 11;
                    // At this point, a is 3, b is 7, and c is 11
                    a += b + c;
                    // At this point, a is 21, b is 7, and c is 11
                };

                // At this point, a is 3, b is 5, and c is not defined
                bar();

                // At this point, a is 21, b is 5
                return 'debuguear para ver';
            }(),
            module: function () {
                String.method('deentityify', function () {
                    // The entity table. It maps entity names to
                    // characters.
                    var entity = {
                        quot: '"',
                        lt: '<',
                        gt: '>'
                    };

                    // Return the deentityify method.
                    return function () {

                        // This is the deentityify method. It calls the string
                        // replace method, looking for substrings that start
                        // with '&' and end with ';'. If the characters in
                        // between are in the entity table, then replace the
                        // entity with the character from the table. It uses
                        // a regular expression (Chapter 7).

                        return this.replace(/&([^&;]+);/g, function (a, b) {
                            var r = entity[b];
                            return typeof r === 'string' ? r : a;
                        });
                    };
                }());
                return "&lt;".deentityify();
            }(),
            cascade: 'decorator pattern',
            memoization: function () {
                var fibonacci = function () {
                    var memo = [1, 1];
                    var calculate = function (n) {
                        var result = memo[n];
                        if (typeof result !== 'number') {
                            result = calculate(n - 1) + calculate(n - 2);
                            memo[n] = result;
                        }
                        return result;
                    };
                    return calculate(arguments[0] - 1); //n>=1
                };
                return fibonacci(6);
            }(),
        };

    </script>
</body>
</html>
